package com.liuhongwei.test.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class PropertiesUtils {

    static Properties properties = new Properties();

    static Map<String, String> PROPER_MAP = new HashMap<String, String>();


    static {
        InputStream is = null;
        try {
            // 包和配置文件都在类路径下面
            is = PropertiesUtils.class.getClassLoader().getResourceAsStream("my-generator/generatorConfig.properties");
            // 加载配置文件
            properties.load(is);
            for (Object o : properties.keySet()) {
                String next = (String) o;
                if (next != null) {
                    PROPER_MAP.put(next, properties.getProperty(next));
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            // 关闭流
            if (is != null){
                try {
                    is.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        }
    }

    public static String getProperty(String key) {
        return PROPER_MAP.get(key);
    }


    public static void main(String[] args) {
        System.out.println(getProperty("db.password"));
    }
}
